<?php
class Home extends Homecontroll{
    private $catelogic;
    private $goodslogic;
    private $cartlogic;
    private $session_id;
    private $orderlogic;
    private $orderitemlogic;
    public function __construct() {
        parent::__construct();
        $this->catelogic=  $this->load("cate");
        $this->goodslogic=  $this->load("goods");
        $this->cartlogic=  $this->load("cart");
        $this->orderlogic=  $this->load("order");
        $this->orderitemlogic=$this->load("order_item");
        $this->session_id=  session_id();
    }
    //网站默认首页
    public function index(){
        $tabcate=  $this->catelogic->select_page("*", "parent_id=0",1,10);
        Temp::assign("tabcate", $tabcate);
        $page=  isset($_REQUEST["page"])?intval($_REQUEST["page"]):1;
        $goods=array();
        $page_data=array();
        foreach ($tabcate as $v){
            $cateid=$v["id"];
            $where="status=1";
            $where.=" and cate_path like '{$cateid},%'";
            $count=  $this->goodslogic->get_count($where);
            $data=$this->goodslogic->select_page("*", $where, $page, 21,"order by id desc");
            $goods[$cateid]=$data;
            $page_data[$cateid] = Common::getPages($count, $page, 21);
            
        }
        //获得商品分类
        $pinfo=array();
         $db=$this->catelogic->select("*"," parent_id=0");
            
         $pinfo[]=array("data"=>$db,"selected"=>0);
          Temp::assign("pinfo", $pinfo);
        
        
        //获得购物车商品数量
        $cart_count=0;
        $cart_cookie=  $this->getCookie("cartlist");
        if(!empty($cart_cookie)){
            $cart_count=  count($cart_cookie);
         }else{
            $cart_count= $this->cartlogic->get_count("session_id='{$this->session_id}'");
         }
         
        Temp::assign("cart_count", $cart_count);
        Temp::assign("page_data", $page_data);
        Temp::assign("goods", $goods);
        Temp::display();
    }
    //清空购物车
    public function clear_cart(){
        $this->_clear_cart();
        return $this->showmessage("清空购物车成功!", "成功信息","/");
    }
    private function _clear_cart(){
        $this->clearCookie("cartlist");
        if($this->session_id){
            $this->cartlogic->delete("session_id='{$this->session_id}'");
        }
    }

    //购物车页面
    public function cartlist(){
        $cart_cookie=  $this->getCookie("cartlist");
        if(empty($cart_cookie)){
           $cart_cookie= $this->cartlogic->select("*","session_id='{$this->session_id}'");
         }
         if(empty($cart_cookie)){
            return $this->showmessage("购物车为空,请先下单!", "异常信息","/"); 
         }
         $zjjg=0.0;
         foreach ($cart_cookie as $k=>$v){
             $goods=  $this->goodslogic->get_one("*","id={$v["good_id"]}");
             $cart_cookie[$k]["goods"]=$goods;
             $zjjg+=$v["sale_weight"]*$v["sale_price"];
         }
        Temp::assign("zjjg", $zjjg);
        Temp::assign("cart_cookie", $cart_cookie);
        Temp::display();
    }
    //生成订单号
    private function genOrderNum(){
        $orderno="OR".date("Ymd").  rand(0, 1000000);
        while ($this->orderlogic->get_count("order_no='{$orderno}'")){
            $orderno="OR".date("Ymd").  rand(0, 1000000);
        }
        return $orderno;
    }

    public function checkout(){
        $info=  isset($_REQUEST["info"])?$_REQUEST["info"]:array();
        $user_name=isset($_REQUEST["user_name"])?$_REQUEST["user_name"]:"undefine";
        $user_phone=isset($_REQUEST["user_phone"])?$_REQUEST["user_phone"]:"";
        $user_address=isset($_REQUEST["user_address"])?$_REQUEST["user_address"]:"";
        if(!$user_name){
            return $this->showmessage("请输入收货人姓名！","错误信息");
        }
        
        
        $order=array(
            "order_no"=>  $this->genOrderNum(),
            "create_time"=>  time(),
            "edit_time"=>  time(),
            "user_name"=>$user_name,
            "user_phone"=>$user_phone,
            "user_address"=>$user_address
        );
       $order_id=  $this->orderlogic->insert($order,TRUE);
        if($order_id){
            $order_item=array();
            $sum_sale_weight=0;
            $sum_sale_price=0;
            $sum_cost_price=0;
            foreach ($info as $k=>$v){
                $goods=  $this->goodslogic->get_one("*","id={$k}");
                $order_item=array(
                    "order_id"=>$order_id,
                    "good_id"=>$v["gid"],
                    "sale_weight"=>$v["sale_weight"],
                    "sale_price"=>$v["sale_price"],
                    "cost_price"=>$goods["market_price"],
                    "good_name"=>$goods["base_name"],
                    "good_image"=>$goods["image"]
                );
                $this->orderitemlogic->insert($order_item);
                $this->goodslogic->update("order_num=order_num+1,sale_weight=sale_weight+{$v["sale_weight"]}","id={$v["gid"]}");
                $sum_sale_weight+=$v["sale_weight"];
                $sum_sale_price+=$v["sale_weight"]*$v["sale_price"];
                $sum_cost_price+=$v["sale_weight"]*$goods["market_price"];
            }
            $order2=array(
                "sale_weight"=>$sum_sale_weight,
                "sale_price"=>$sum_sale_price,
                "cost_price"=>$sum_cost_price,
            );
            $this->orderlogic->update($order2,"id={$order_id}");
            $this->_clear_cart();
        }
        return $this->showmessage("结算完毕!","成功信息","/");
    }

        //商品详细页
    public function detail(){
        $gid=isset($_REQUEST["gid"])?intval($_REQUEST["gid"]):0;
        $goods=$this->goodslogic->get_one("*","id={$gid}");
        $cateids=  explode(",", $goods["cate_path"]);
         $catename_array=array();
            foreach ($cateids as $v){
                if($v){
                    $cate=  $this->catelogic->get_one("*","id={$v}");
                    $catename_array[]=$cate["base_name"];
                }
            }
            $goods["cate_name"]=implode(" > ", $catename_array);
            
            //商品分类
             $pinfo=array();
             $db=$this->catelogic->select("*"," parent_id=0");
             
             $pinfo[]=array("data"=>$db,"selected"=>0);
             Temp::assign("pinfo", $pinfo);
            
        Temp::assign("goods", $goods);
        
        //获得购物车商品数量
        $cart_count=0;
        $cart_cookie=  $this->getCookie("cartlist");
        if(!empty($cart_cookie)){
            $cart_count=  count($cart_cookie);
         }else{
            $cart_count= $this->cartlogic->get_count("session_id='{$this->session_id}'");
         }
        Temp::assign("cart_count", $cart_count);
        Temp::display();
    }






    //搜索页面
   public function search(){
       if(isset($_REQUEST["search"])){
            $where="1=1";
            $base_name=  isset($_REQUEST["base_name"])?$_REQUEST["base_name"]:"";
           $parent_id=isset($_REQUEST["parent_id"])?$_REQUEST["parent_id"]:0;
           if($base_name){
               $where.=" and (base_name like '%{$base_name}%' or sx like'".strtolower($base_name)."%')";
           }
           if(!empty($parent_id)&&isset($parent_id[0])){
               if($parent_id[0]!=0){
                   
          
                   
                   
                   
                   
               foreach ($parent_id as $k=>$val){
                   if($val==-1){
                        unset($parent_id[$k]);
                    }
               }
               $cate_str=  implode(",", $parent_id);
               $cate_str=$cate_str.",";
                $where.=" and cate_path like '{$cate_str}%' ";
                  $goodscate=$parent_id;
            $pinfo=array();
            foreach($goodscate as $val){
                if($val){
                    $cate=  $this->catelogic->get_one("*","id={$val}");
                    $catelist=$this->catelogic->select("*","parent_id={$cate["parent_id"]}");
                    $data=array("data"=>$catelist,"selected"=>$cate["id"]);
                    $pinfo[]=$data;
                }
            }
         Temp::assign("pinfo", $pinfo);
            
                
                
                
               }  else {
                    $pinfo=array();
                   $db=$this->catelogic->select("*"," parent_id=0");

                   $pinfo[]=array("data"=>$db,"selected"=>0);
                   Temp::assign("pinfo", $pinfo);
               }
           }else{
             $pinfo=array();
             $db=$this->catelogic->select("*"," parent_id=0");
             
             $pinfo[]=array("data"=>$db,"selected"=>0);
             Temp::assign("pinfo", $pinfo);
           }
           
       }else{
             $pinfo=array();
             $db=$this->catelogic->select("*"," parent_id=0");
             
             $pinfo[]=array("data"=>$db,"selected"=>0);
             Temp::assign("pinfo", $pinfo);
       }
       
       
       
       
       $page=  isset($_REQUEST["page"])?intval($_REQUEST["page"]):1;
        $count=  $this->goodslogic->get_count($where);
        $list=$this->goodslogic->select_page("*", $where, $page,21,"order by id desc");
      
        if($list){
        foreach ($list as $k=>$val){
            $cateids=  explode(",", $val["cate_path"]);
            $catename_array=array();
            foreach ($cateids as $v){
                if($v){
                    $cate=  $this->catelogic->get_one("*","id={$v}");
                    $catename_array[]=$cate["base_name"];
                }
            }
            $list[$k]["thumb"]=  $this->getThumb($val["image"], "100x100");
            $list[$k]["cate_name"]=  implode(" > ", $catename_array);
        }
        $page_data = Common::getPages($count, $page, 21);
        Temp::assign("page_data", $page_data);
        Temp::assign("list", $list);
        }
       
       
       //获得购物车商品数量
        $cart_count=0;
        $cart_cookie=  $this->getCookie("cartlist");
        if(!empty($cart_cookie)){
            $cart_count=  count($cart_cookie);
         }else{
            $cart_count= $this->cartlogic->get_count("session_id='{$this->session_id}'");
         }
        Temp::assign("cart_count", $cart_count);
       Temp::display();
   }

    //ajax分页
    public function public_ajax_pagelist(){
            $cateid=isset($_REQUEST["cateid"])?intval($_REQUEST["cateid"]):0;
            $page=  isset($_REQUEST["page"])?intval($_REQUEST["page"]):1;
            $where="status=1";
            $where.=" and cate_path like '{$cateid},%'";
            $count=  $this->goodslogic->get_count($where);
            $data=$this->goodslogic->select_page("*", $where, $page, 21,"order by id desc");
            $page_data= Common::getPages($count, $page, 21);
            Temp::assign("cateid", $cateid);
            Temp::assign("data", $data);
            Temp::assign("page_data", $page_data);
            Temp::display();
    }

    //删除购物车里面的商品
    public function public_ajax_delete_cart_id(){
        $gid=isset($_REQUEST["gid"])?intval($_REQUEST["gid"]):0;
        $msg=array();
        if($gid){
            $cart_cookie=  $this->getCookie("cartlist");
            if(!empty($cart_cookie)){
                foreach ($cart_cookie as $k=>$val){
                    if($val["good_id"]==$gid){
                        unset($cart_cookie[$k]);
                    }
                }
                setcookie("cartlist", serialize($cart_cookie), time()+3600*24);
                
             }
             $this->cartlogic->delete("session_id='{$this->session_id}' and good_id={$gid}");
             $msg["status"]=1;
             echo json_encode($msg);
             return ;
        }else{
            $msg["status"]=-1;
             echo json_encode($msg);
             return ;
        }
    }




    //加入购物车
    public function public_ajax_add_cart(){
        $gid=isset($_REQUEST["gid"])?intval($_REQUEST["gid"]):0;
        $sale_price=isset($_REQUEST["dj"])?  floatval($_REQUEST["dj"]):0;
        $sale_weight=isset($_REQUEST["zl"])?  floatval($_REQUEST["zl"]):0;
        $msg=array();
        if(!$gid){
            $msg["status"]=-1;
            echo json_encode($msg);
            return;
        }
        $goods=  $this->goodslogic->get_one("*","id={$gid}");
        $cart=  $this->cartlogic->get_one("*","good_id={$gid} and session_id='{$this->session_id}'");
        $cart_cookie=  $this->getCookie("cartlist");
        if(empty($cart_cookie)){
            $cart_cookie[$gid]=array("good_id"=>$gid,"sale_weight"=>$sale_weight,"sale_price"=>$sale_price);
            setcookie("cartlist", serialize($cart_cookie), time()+3600*24);
        }else{
            if(!isset($cart_cookie[$gid])){
                $cart_cookie[$gid]=array("sale_weight"=>0.0,"sale_price"=>0.0,"good_id"=>$gid);
            }
            $cart_cookie[$gid]["sale_weight"]=$cart_cookie[$gid]["sale_weight"]+$sale_weight;
            $cart_cookie[$gid]["sale_price"]=$sale_price;
            setcookie("cartlist", serialize($cart_cookie), time()+3600*24);
        }
        if(!$cart){
            $cartinfo=array(
                "good_id"=>$gid,
                "session_id"=>  $this->session_id,
                "sale_weight"=>$sale_weight,
                "sale_price"=>$sale_price,
                "cost_price"=>$goods["market_price"],
                "good_name"=>$goods["base_name"],
                "good_image"=>$goods["image"],
                "create_time"=>  time()
            );
            $this->cartlogic->insert($cartinfo);
            
        }else{
            $cart["sale_weight"]=$cart["sale_weight"]+$sale_weight;
            $cart["sale_price"]=$sale_price;
            $cart["cost_price"]=$goods["market_price"];
            $cart["good_name"]=$goods["base_name"];
            $cart["good_image"]=$goods["image"];
            $this->cartlogic->update($cart,"id={$cart["id"]}");
            
        }
         $msg["status"]=1;
         if(!empty($cart_cookie)){
            $msg["cart_count"]=  count($cart_cookie);
         }else{
            $msg["cart_count"]= $this->cartlogic->get_count("session_id='{$this->session_id}'");
         }
         echo json_encode($msg);
        
    }
}
